package com.scada.service.impl;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.scada.contant.MessageConstants;
import com.scada.hessian.client.impl.PermissionVisitorClient;
import com.scada.model.bean.Permission;
import com.scada.model.result.PageInfo;
import com.scada.model.result.ResponseBean;
import com.scada.service.PermissionService;



@Service
public class PermissionServiceImpl implements PermissionService{
	
	@Autowired
	private PermissionVisitorClient permissionVisitorClient;

	@Override
	public PageInfo<Permission> selectByPrimaryKey(PageInfo<Permission> pageInfo) {
		
		return permissionVisitorClient.selectByPrimaryKey(pageInfo);
	}

	@Override
	public Boolean validateIndex(Permission permission) {
		
		return permissionVisitorClient.validateIndex(permission);
	}

	@Override
	public ResponseBean deleteByPrimaryKey(String ids) {
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("state", MessageConstants.State.STATE_DEL.getKey());
		List<Integer> empNoList=new ArrayList<Integer>();
		for(String empno : ids.split(","))
		{
			empNoList.add(Integer.parseInt(empno));
		}
		map.put("empNoList", empNoList);
		return permissionVisitorClient.deleteByPrimaryKey(map);
	}

	@Override
	public ResponseBean insertSelective(Permission permission) {
		SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		permission.setState(MessageConstants.State.STATE_SHOW.getKey());
		permission.setCreateTime(sFormat.format(new Date()));
		return permissionVisitorClient.insertSelective(permission);
	}

	@Override
	public ResponseBean updateByPrimaryKeySelective(Permission permission) {
		
		return permissionVisitorClient.updateByPrimaryKeySelective(permission);
	}

	@Override
	public Permission selectById(int id) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("state", MessageConstants.State.STATE_SHOW.getKey());
		map.put("id", id);
		return permissionVisitorClient.selectById(map);
	}

	@Override
	public List<String> getPermissionsName(Integer roleId) {
		List<Permission> perlist= permissionVisitorClient.getPermissionsName(roleId);
		List<String> list=new ArrayList<String>(); 
		 for (Permission per : perlist) {  
	            list.add(per.getPermissionName());  
	        }  
	        return list;
	}

	@Override
	public List<String> getPermissionsCode(Integer roleId) {
		
		List<Permission> perlist= permissionVisitorClient.getPermissionsCode(roleId);
		List<String> list=new ArrayList<String>(); 
		for (Permission per : perlist) {  
            list.add(per.getPermissionCode());  
        }  
        return list;
	}

}
